﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKHACHHANG.DanhMucForm;
using QLKHACHHANG.DataAccess;

namespace QLKHACHHANG
{
    public partial class frmStaffConfig : DevExpress.XtraEditors.XtraForm
    {
        
         frmStaff rootform;
        bool isupdate = false;
        public frmStaffConfig(frmStaff root, bool isup)
        {
            InitializeComponent();
            rootform = root;
            isupdate = isup;            
            if (isupdate)
            {
                StaffInfo();
            }
        }
        public void SetTitle(string name)
        {
            this.Text = name;
        }

        private void frmStaffConfig_Load(object sender, EventArgs e)
        {
            
            try
            {
                // TODO: This line of code loads data into the 'ds_Bophan.tbl_BoPhan' table. You can move, or remove it, as needed.
                this.tbl_BoPhanTableAdapter.Connection.ConnectionString = SetConfig.getAppSetting("ConnectionString");
                this.tbl_BoPhanTableAdapter.Fill(this.ds_Bophan.tbl_BoPhan);
             }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại ! \n\n (" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        // show thong tin
        private void StaffInfo()
        {
            txtManhanvien.Enabled = false;
            txtManhanvien.Text = rootform.gvStaff.GetFocusedDataRow()["Manhanvien"].ToString();
            txtTennhanvien.Text = rootform.gvStaff.GetFocusedDataRow()["Tennhanvien"].ToString();
            txtTendangnhap.Text = rootform.gvStaff.GetFocusedDataRow()["Tendangnhap"].ToString();
            txtMatkhau.Text = rootform.gvStaff.GetFocusedDataRow()["Matkhau"].ToString();
            txtChucvu.Text = rootform.gvStaff.GetFocusedDataRow()["Chucvu"].ToString();
            txtDiachi.Text = rootform.gvStaff.GetFocusedDataRow()["Diachi"].ToString();
            txtSodienthoai.Text = rootform.gvStaff.GetFocusedDataRow()["Sodienthoai"].ToString();
            txtEmail.Text = rootform.gvStaff.GetFocusedDataRow()["Email"].ToString();
            lkBophan.EditValue = rootform.gvStaff.GetFocusedDataRow()["Mabophan"].ToString();            
             gpTrangthai.SelectedIndex = rootform.gvStaff.GetFocusedDataRow()["Trangthai"].ToString().Equals("True")?1:0;                            
        }

        public void UpdateBophan()
        {

            try
            {
                this.tbl_BoPhanTableAdapter.Fill(this.ds_Bophan.tbl_BoPhan);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cập nhật danh sách thất bại ! \n\n (" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
         private void ResetControl()
        {
             txtManhanvien.Focus();
             txtManhanvien.Text = "";
             txtTennhanvien.Text = "";
             txtTendangnhap.Text = "";       
             txtMatkhau.Text = "";
             lkBophan.EditValue = "-1";
             txtChucvu.Text = "";
             txtDiachi.Text = "";
             txtEmail.Text = "";
             gpTrangthai.SelectedIndex = 0;                     
        }       
        private void lkBophan_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                frmBoPhan frm = new frmBoPhan(this);
                frm.ShowDialog();
            }
        }

        private void frmStaffConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát khỏi màn hình " + this.Text + " ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            if (isupdate)
            {
                // update nhan vien
                
                if (txtTennhanvien.Text.Equals("") || lkBophan.Text.Equals("") || txtTendangnhap.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng điền thông tin hàng đầy đủ. Dấu * là phần bắt buộc!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {
                    int luachon = rootform.gvStaff.GetFocusedDataRow()["Tendangnhap"].ToString().Equals(txtTendangnhap.Text) ? 0: 1;
                    if (DBStaff.UpdateStaff(
                    txtManhanvien.Text,
                    txtTennhanvien.Text,
                    txtTendangnhap.Text,
                    Utilities.Utilities.StringToMD5(txtMatkhau.Text),
                    txtChucvu.Text,
                    txtDiachi.Text,
                    txtSodienthoai.Text,
                    txtEmail.Text,
                    lkBophan.EditValue.ToString(),
                    gpTrangthai.SelectedIndex,
                    luachon
                    ))
                    {
                        MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        rootform.UpdateView();

                    }                   
                }

                
            }
            else
            {
                if (txtManhanvien.Text.Equals("") || txtTennhanvien.Text.Equals("") || lkBophan.Text.Equals("") || txtTendangnhap.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng điền thông tin hàng đầy đủ. Dấu * là phần bắt buộc!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {

                    // insert 1 khach hang moi
                    if (DBStaff.InsertStaff(
                    txtManhanvien.Text,
                    txtTennhanvien.Text,
                    txtTendangnhap.Text,
                    Utilities.Utilities.StringToMD5(txtMatkhau.Text),
                    txtChucvu.Text,
                    txtDiachi.Text,
                    txtSodienthoai.Text,
                    txtEmail.Text,
                    lkBophan.EditValue.ToString(),
                    gpTrangthai.SelectedIndex
                    ))
                    {
                        MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        rootform.UpdateView();
                        ResetControl();

                    }

                }
            }
        }

    }
}